{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Rings and Rods"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countPoints"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #环和杆"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>总计有 <code>n</code> 个环，环的颜色可以是红、绿、蓝中的一种。这些环分别穿在 10 根编号为 <code>0</code> 到 <code>9</code> 的杆上。</p>\n",
    "\n",
    "<p>给你一个长度为 <code>2n</code> 的字符串 <code>rings</code> ，表示这 <code>n</code> 个环在杆上的分布。<code>rings</code> 中每两个字符形成一个 <strong>颜色位置对</strong> ，用于描述每个环：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>第 <code>i</code> 对中的 <strong>第一个</strong> 字符表示第 <code>i</code> 个环的 <strong>颜色</strong>（<code>'R'</code>、<code>'G'</code>、<code>'B'</code>）。</li>\n",
    "\t<li>第 <code>i</code> 对中的 <strong>第二个</strong> 字符表示第 <code>i</code> 个环的 <strong>位置</strong>，也就是位于哪根杆上（<code>'0'</code> 到 <code>'9'</code>）。</li>\n",
    "</ul>\n",
    "\n",
    "<p>例如，<code>\"R3G2B1\"</code> 表示：共有 <code>n == 3</code> 个环，红色的环在编号为 3 的杆上，绿色的环在编号为 2 的杆上，蓝色的环在编号为 1 的杆上。</p>\n",
    "\n",
    "<p>找出所有集齐 <strong>全部三种颜色</strong> 环的杆，并返回这种杆的数量。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/11/23/ex1final.png\" style=\"width: 258px; height: 130px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>rings = \"B0B6G0R6R0R6G9\"\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>\n",
    "- 编号 0 的杆上有 3 个环，集齐全部颜色：红、绿、蓝。\n",
    "- 编号 6 的杆上有 3 个环，但只有红、蓝两种颜色。\n",
    "- 编号 9 的杆上只有 1 个绿色环。\n",
    "因此，集齐全部三种颜色环的杆的数目为 1 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/11/23/ex2final.png\" style=\"width: 266px; height: 130px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>rings = \"B0R0G0R9R0B0G0\"\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>\n",
    "- 编号 0 的杆上有 6 个环，集齐全部颜色：红、绿、蓝。\n",
    "- 编号 9 的杆上只有 1 个红色环。\n",
    "因此，集齐全部三种颜色环的杆的数目为 1 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>rings = \"G4\"\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>\n",
    "只给了一个环，因此，不存在集齐全部三种颜色环的杆。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>rings.length == 2 * n</code></li>\n",
    "\t<li><code>1 &lt;= n &lt;= 100</code></li>\n",
    "\t<li>如 <code>i</code> 是 <strong>偶数</strong> ，则&nbsp;<code>rings[i]</code> 的值可以取 <code>'R'</code>、<code>'G'</code> 或 <code>'B'</code>（下标从 <strong>0</strong> 开始计数）</li>\n",
    "\t<li>如 <code>i</code> 是 <strong>奇数</strong> ，则&nbsp;<code>rings[i]</code> 的值可以取 <code>'0'</code> 到 <code>'9'</code> 中的一个数字（下标从 <strong>0</strong> 开始计数）</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [rings-and-rods](https://leetcode.cn/problems/rings-and-rods/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [rings-and-rods](https://leetcode.cn/problems/rings-and-rods/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"B0B6G0R6R0R6G9\"', '\"B0R0G0R9R0B0G0\"', '\"G4\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        n = len(rings)\n",
    "        count = [set() for _ in range(10)]\n",
    "        for i in range(0, n, 2):\n",
    "            count[int(rings[i+1])].add(rings[i])\n",
    "        res = 0\n",
    "        for s in count:\n",
    "            if len(s) > 2:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        ringlist=[0]*10\n",
    "        color={'R':1,'G':2,'B':4}\n",
    "        for i in range(int(len(rings)/2)):\n",
    "            ringlist[int(rings[2*i+1])]=ringlist[int(rings[2*i+1])]|color[rings[2*i]]\n",
    "        return ringlist.count(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        #color_index , ring_index = 0,1\n",
    "        count = [[0,0,0] for _ in range(10)]\n",
    "        for i in range(len(rings)//2):\n",
    "            count[int(rings[2*i+1])][self.get_num_from_color(rings[2*i]) ] = 1\n",
    "        count_ring = 0\n",
    "        for colors in count:\n",
    "            c = 1\n",
    "            for color in colors:\n",
    "                c &= color\n",
    "            count_ring += c\n",
    "        return count_ring\n",
    "\n",
    "    def get_num_from_color(self,color):\n",
    "        if color == 'R':\n",
    "            return 0\n",
    "        elif color == 'G':\n",
    "            return 1\n",
    "        else:\n",
    "            return 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def countPoints(self, rings: str) -> int:\n",
    "#         sets = [set() for _ in range(10)]\n",
    "#         for i in range(0, len(rings), 2):\n",
    "#             color = rings[i]\n",
    "#             pole = rings[i + 1]\n",
    "#             sets[int(pole)].add(color)\n",
    "#         res = 0\n",
    "#         for s in sets:\n",
    "#             if len(s) == 3:\n",
    "#                 res += 1\n",
    "#         return res\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        sets = {}\n",
    "        for i in range(0, len(rings), 2):\n",
    "            color = rings[i]\n",
    "            pole = rings[i + 1]\n",
    "            if pole not in sets:\n",
    "                sets[pole] = set()\n",
    "            sets[pole].add(color)\n",
    "        res = 0\n",
    "        for s in sets.values():\n",
    "            if len(s) == 3:\n",
    "                res += 1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, s: str) -> int:\n",
    "        n, ans = len(s), 0\n",
    "        map = [0] * 128\n",
    "        for i in range(0, n, 2):\n",
    "            map[ord(s[i]) - ord('B')] |= 1 << (int(s[i + 1]) - int('0'))\n",
    "        for i in range(10):\n",
    "            tot = 0\n",
    "            for c in ['R', 'G', 'B']:\n",
    "                tot += (map[ord(c) - ord('B')] >> i) & 1\n",
    "            ans += 1 if tot == 3 else 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        r = [0] * 10\n",
    "        for i in range(0, len(rings), 2):\n",
    "            if rings[i] == 'R':\n",
    "                r[int(rings[i+1])] |= 4\n",
    "            elif rings[i] == 'G':\n",
    "                r[int(rings[i+1])] |= 2\n",
    "            elif rings[i] == 'B':\n",
    "                r[int(rings[i+1])] |= 1\n",
    "        return len([i for i in r if i == 7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        i = 0\n",
    "        n = len(rings)\n",
    "        l = [[0,0,0] for i in range(10)]\n",
    "        change = {'R':0,'G':1,'B':2}\n",
    "        while i<n:\n",
    "            C = rings[i]\n",
    "            I = int(rings[i+1])\n",
    "            i+=2\n",
    "            l[I][change[C]] = 1\n",
    "        ans = 0\n",
    "        for a,b,c in l:\n",
    "            if a==b==c==1:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        # 创建一个字典来记录每根杆上各种颜色环的数量\n",
    "        poles = {str(i): set() for i in range(10)}\n",
    "        \n",
    "        # 遍历输入字符串rings，更新字典\n",
    "        for i in range(0, len(rings), 2):\n",
    "            color, pole = rings[i], rings[i + 1]\n",
    "            poles[pole].add(color)\n",
    "        \n",
    "        # 统计具有三种颜色环的杆的数量\n",
    "        count = 0\n",
    "        for pole_colors in poles.values():\n",
    "            if len(pole_colors) == 3:\n",
    "                count += 1\n",
    "        \n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        mask = [0] * 10\n",
    "        d = {\"R\": 1, \"G\": 2, \"B\": 4}\n",
    "        for i in range(0, len(rings), 2):\n",
    "            c = rings[i]\n",
    "            j = int(rings[i + 1])\n",
    "            mask[j] |= d[c]\n",
    "        return mask.count(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "\n",
    "        n = len(rings)\n",
    "        _dict = defaultdict(set)\n",
    "        for i in range(0, n, 2):\n",
    "            color = rings[i]\n",
    "            index = int(rings[i + 1])\n",
    "            _dict[index + 1].add(color)\n",
    "        \n",
    "        result = 0\n",
    "        for _, val in _dict.items():\n",
    "            if len(val) == 3:\n",
    "                result += 1\n",
    "        return result\n",
    "        \n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        POLE_NUM = 10\n",
    "        mapping = {'R' : 0, 'G' : 1, 'B' : 2}\n",
    "        state = [0] * POLE_NUM\n",
    "        for i in range (0, len(rings), 2):\n",
    "            color = rings[i]\n",
    "            pole_index = ord(rings[i + 1]) - ord('0')\n",
    "            state[pole_index] |= 1 << mapping[color]\n",
    "        return sum(state[i] == 7 for i in range(POLE_NUM))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        # 对相应的杆子序号，进行或运算0与(1/2/4)\n",
    "        res = [0] * (10)\n",
    "        for i in range(0,len(rings),2):  # 颜色\n",
    "            gan = int(rings[i+1])\n",
    "            if rings[i] == \"R\":\n",
    "                res[gan] |= 1\n",
    "            elif rings[i] == \"G\":\n",
    "                res[gan] |= 2\n",
    "            else:\n",
    "                res[gan] |= 4\n",
    "        return len([i for i in res if i == 7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def countPoints(self, rings):\n",
    "        j = 0\n",
    "        for i in range(10):\n",
    "            red = \"R\"+str(i)\n",
    "            blue = \"B\"+str(i)\n",
    "            green = \"G\"+str(i)            \n",
    "            if ((rings.find(red)!=-1)and(rings.find(blue)!=-1)and(rings.find(green)!=-1)):\n",
    "                j += 1\n",
    "        return j\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        poles = [0] * 10\n",
    "        maps = {'R':0, 'G':1, 'B':2}\n",
    "        for i in range(0, len(rings), 2):\n",
    "            poles[int(rings[i + 1])] |= 1 << maps[rings[i]]\n",
    "        return sum(p == 7 for p in poles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        a={}\n",
    "        b=[]\n",
    "        c=[]\n",
    "        for i in rings:\n",
    "            if i.isalpha():\n",
    "                b.append(i)\n",
    "            else:\n",
    "                c.append(i)\n",
    "        for n,i in enumerate(c):\n",
    "            if i not in a:\n",
    "                a[i]=[b[n]]\n",
    "            else:\n",
    "                a[i].append(b[n])\n",
    "        c=0\n",
    "        for k,v in a.items():\n",
    "            if len(set(v))==3:\n",
    "                c+=1\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        s = [0] * 10\n",
    "        m = {'R':1, 'G':2, 'B':4}\n",
    "        for i in range(0, len(rings), 2):\n",
    "            s[int(rings[i+1])] |= m[rings[i]]\n",
    "        cnt = 0\n",
    "        for i in s:\n",
    "            if i == 7:\n",
    "                cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "bin={'R':1,'B':2,'G':4}\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        cnt=defaultdict(int)\n",
    "        for i in range(len(rings)//2):\n",
    "            cnt[rings[2*i+1]]|=bin[rings[2*i]]\n",
    "        return sum(k==7 for k in cnt.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        dicts={i:[] for i in range(10)}\n",
    "        sum=0\n",
    "        for i in range(0,len(rings),2):\n",
    "            dicts[int(rings[i+1])].append(rings[i])\n",
    "        for k in dicts:\n",
    "            if 'R' in dicts[k] and 'G' in dicts[k] and 'B' in dicts[k]:\n",
    "                sum+=1\n",
    "        return sum\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "\n",
    "        nums_out = 0\n",
    "\n",
    "        dict_status = {}\n",
    "\n",
    "        #一旦集齐就提前跳出而不是一直到结束\n",
    "\n",
    "        for temp in range(0,len(rings),2):\n",
    "            color_temp = rings[temp]\n",
    "            pos_temp = rings[temp+1]\n",
    "\n",
    "            if dict_status.get(pos_temp)!=None:\n",
    "                if color_temp not in dict_status[pos_temp]:\n",
    "                    dict_status[pos_temp].append(color_temp)\n",
    "                else:\n",
    "                    pass\n",
    "            \n",
    "            else:\n",
    "                dict_status[pos_temp]=[color_temp]\n",
    "\n",
    "        # return dict_status\n",
    "        for d in dict_status:\n",
    "            if len(dict_status[d])>=3:\n",
    "                nums_out +=1\n",
    "\n",
    "        return nums_out\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        count_map = {\n",
    "            i: 0\n",
    "            for i in range(10)\n",
    "        }\n",
    "        ans = 0\n",
    "        for n in range(len(rings) // 2):\n",
    "            cur_ring = rings[2*n: (2*n + 2)]\n",
    "            color = cur_ring[0]\n",
    "            index = int(cur_ring[1])\n",
    "            if count_map[index] == 7:\n",
    "                continue\n",
    "            if color == 'R':\n",
    "                count_map[index] |= 1\n",
    "            elif color == 'B':\n",
    "                count_map[index] |= 2\n",
    "            else:\n",
    "                count_map[index] |= 4\n",
    "            if count_map[index] == 7:\n",
    "                ans += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, s: str) -> int:\n",
    "        n, ans = len(s), 0\n",
    "        map = [0] * 128\n",
    "        for i in range(0, n, 2):\n",
    "            map[ord(s[i]) - ord('B')] |= 1 << (int(s[i + 1]) - int('0'))\n",
    "        for i in range(10):\n",
    "            tot = 0\n",
    "            for c in ['R', 'G', 'B']:\n",
    "                tot += (map[ord(c) - ord('B')] >> i) & 1\n",
    "            ans += 1 if tot == 3 else 0\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        dic = {'R': 4, 'G': 2, 'B': 1}\n",
    "        s = [0] * 10\n",
    "        for i in range(0, len(rings), 2):\n",
    "            index = rings[i+1]\n",
    "            c = dic[rings[i]]\n",
    "            s[int(index)] |= c\n",
    "        return s.count(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        dic = {str(i):set() for i in range(10)}\n",
    "        count = 0\n",
    "        for idx in range(0,len(rings),2):\n",
    "            if rings[idx+1] not in dic:\n",
    "                continue\n",
    "            dic[rings[idx+1]].add(rings[idx])\n",
    "            if len(dic[rings[idx+1]]) == 3:\n",
    "                count += 1\n",
    "                del dic[rings[idx+1]]\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        mp = [0]*10\n",
    "        for x in [rings[2*i:2*i+2] for i in range(len(rings)//2)]:\n",
    "            mp[int(x[1])]|= 4 if x[0]=='B' else 2 if x[0]=='G' else 1\n",
    "        return len([_ for _ in filter(lambda x:x==7, mp)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        a = [0]*10\n",
    "        mapping = {\"R\":0,\"G\":1,\"B\":2}\n",
    "        for i in range(0,len(rings),2):\n",
    "            color = rings[i]\n",
    "            gan = int(rings[i+1])\n",
    "            a[gan] |= 1 << mapping[color]\n",
    "        return a.count(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "\n",
    "        \"\"\"\n",
    "        有 n 個圓環, 每個圓環的顏色有: R, G, B\n",
    "        這 n 個圓環分散在 10 個桿子上面(0~9)\n",
    "\n",
    "        給一個 string: rings\n",
    "        1. 長度是 2n\n",
    "        2. 裡面紀錄的資訊是: <哪個顏色><哪個桿子>\n",
    "        \n",
    "        回傳有幾個桿子上面是有 R, G, B 三種圓環\n",
    "        \"\"\"\n",
    "\n",
    "        \"\"\"\n",
    "        1. 做一個 dict, key = rod_num, value = set(三種顏色)\n",
    "        2. 走過一次 rings, 紀錄 key, value\n",
    "        3. 檢查 dict 每個 key 的 set 長度是否等於 3\n",
    "           是的話就累積次數 \n",
    "        \"\"\"\n",
    "        rods = collections.defaultdict(set)\n",
    "\n",
    "        for i in range(0, len(rings), 2):\n",
    "            color   = rings[i]\n",
    "            rod_num = rings[i + 1]\n",
    "            #print(f\"{color=}, {rod_num=}\")\n",
    "            rods[rod_num].add(color)\n",
    "        \n",
    "        #print(rods)\n",
    "        result = 0\n",
    "        for i in rods:\n",
    "            if len(rods[i]) == 3:\n",
    "                result += 1\n",
    "        return result "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        cnt = defaultdict(set)\n",
    "        for j in range(0, len(rings), 2):\n",
    "            cnt[rings[j+1]].add(rings[j])\n",
    "        \n",
    "        return sum(True for k, v in cnt.items() if len(v) == 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        if len(rings) < 6:\n",
    "            return 0\n",
    "        count = defaultdict(set)\n",
    "        for i in range(len(rings)):\n",
    "            if rings[i].isalpha():\n",
    "                count[rings[i + 1]].add(rings[i])\n",
    "            else:\n",
    "                continue\n",
    "        \n",
    "        result = 0\n",
    "        for k, v in count.items():\n",
    "            if len(v) == 3:\n",
    "                result += 1\n",
    "        \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        rods = [0] * 10   # 统计每个杆出现的环的颜色\n",
    "        n = len(rings)\n",
    "        for i in range(1, n, 2):  # 枚举每一个颜色位置对\n",
    "            rodIdx = int(rings[i])    # 获取杆的位置\n",
    "            # 更新杆的颜色\n",
    "            if rings[i-1] == 'R':\n",
    "                rods[rodIdx] |= 1\n",
    "            elif rings[i-1] == 'G':\n",
    "                rods[rodIdx] |= 2\n",
    "            else:\n",
    "                rods[rodIdx] |= 4\n",
    "        # 统计出现了三种环的杆\n",
    "        return len([i for i in rods if i == 7])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "\n",
    "        \"\"\"\n",
    "        有 n 個圓環, 每個圓環的顏色有: R, G, B\n",
    "        這 n 個圓環分散在 10 個桿子上面(0~9)\n",
    "\n",
    "        給一個 string: rings\n",
    "        1. 長度是 2n\n",
    "        2. 裡面紀錄的資訊是: <哪個顏色><哪個桿子>\n",
    "        \n",
    "        回傳有幾個桿子上面是有 R, G, B 三種圓環\n",
    "        \"\"\"\n",
    "\n",
    "        \"\"\"\n",
    "        1. 做一個 dict\n",
    "           key = rod_num\n",
    "           value = set(三種顏色), 用 set 不重複\n",
    "        2. 走過一次 rings, 紀錄 key, value\n",
    "        3. 檢查 dict 每個 key 的 set 長度是否等於 3\n",
    "           是的話就累積次數 \n",
    "        \"\"\"\n",
    "        rods = collections.defaultdict(set)\n",
    "\n",
    "        for i in range(0, len(rings), 2):\n",
    "            color   = rings[i]\n",
    "            rod_num = rings[i + 1]\n",
    "            #print(f\"{color=}, {rod_num=}\")\n",
    "            rods[rod_num].add(color)\n",
    "        \n",
    "        #print(rods)\n",
    "        result = 0\n",
    "        for i in rods:\n",
    "            if len(rods[i]) == 3:\n",
    "                result += 1\n",
    "        return result "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        a = 0\n",
    "        p = {'B': 0, 'G': 1, 'R': 2}\n",
    "        for i in range(0, len(rings), 2):\n",
    "        # for c, pos in itertools.batched(rings, 2):\n",
    "            a |= (1 << p[rings[i]]) << (3 * int(rings[i+1]))\n",
    "            # a |= (1 << p[c]) << (3 * int(pos))\n",
    "        return sum((a >> (3 * k) & 0b111) == 0b111 for k in range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        dic = {str(i):[] for i in range(10)}\n",
    "        ans = 0\n",
    "        for idx in range(0,len(rings),2):\n",
    "            dic[rings[idx+1]].append(rings[idx])\n",
    "        for key in dic:\n",
    "            if len(set(dic[key])) >= 3:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        arr = ['' for i in range(10)]\n",
    "        i = 0\n",
    "        while i < len(rings)-1:\n",
    "            tmp = rings[i:i+2]\n",
    "            num = int(tmp[1])\n",
    "            arr[num] += tmp[0]\n",
    "            i += 2\n",
    "        count = 0\n",
    "        for s in arr:\n",
    "            if 'R' in s and 'G' in s and 'B' in s:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        rings_len = len(rings)\n",
    "        hashtable = defaultdict(set)\n",
    "        for i in range(0,rings_len,2):\n",
    "            color = rings[i]\n",
    "            idx = rings[i+1]\n",
    "            hashtable[idx].add(color)\n",
    "        return sum([1 for i in hashtable.values() if i == set('RGB')])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        array = [[] for _ in range(10)]\n",
    "        for i in range(0, len(rings), 2):\n",
    "            color = rings[i]\n",
    "            num = int(rings[i + 1])\n",
    "            if color not in array[num]:\n",
    "                array[num].append(color)\n",
    "        res = 0\n",
    "        for a in array:\n",
    "            if len(a) == 3:\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        ans = 0\n",
    "        if len(rings)<6:\n",
    "            return ans\n",
    "        \n",
    "        temp = [[0,0,0] for _ in range(10)]\n",
    "        #print(temp)\n",
    "        for i in range(0,len(rings)-1,2):\n",
    "            j = int(rings[i+1])\n",
    "            #print(j)\n",
    "            if rings[i] == \"R\":\n",
    "                temp[j][0] = 1\n",
    "            if rings[i] == \"G\":\n",
    "                temp[j][1] = 1\n",
    "            if rings[i] == \"B\":\n",
    "                temp[j][2] = 1\n",
    "        for i in range(10):\n",
    "            if temp[i] == [1,1,1]:\n",
    "                ans+=1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        dct = defaultdict(set)\n",
    "        for c , i  in zip(rings[::2],rings[1::2]):dct[i].add(c)\n",
    "        return list(dct.values()).count({'R','G','B'})\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        color_dic={}\n",
    "        for i in range(0,len(rings),2):\n",
    "            if rings[i+1] not in color_dic:\n",
    "                color_dic[rings[i+1]]=''\n",
    "            if rings[i] not in color_dic[rings[i+1]]:\n",
    "                color_dic[rings[i+1]]+=rings[i]\n",
    "        num=0\n",
    "        for i in color_dic:\n",
    "            if len(color_dic[i])==3:\n",
    "                num+=1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        '''\n",
    "        位运算表示状态\n",
    "        '''\n",
    "\n",
    "        rec = [0] * 10\n",
    "        mapping = {i:j for i,j in zip('RGB', [0,1,2])}\n",
    "        for i in range(0, len(rings), 2):\n",
    "            c = rings[i]\n",
    "            index = int(rings[i + 1])\n",
    "\n",
    "            rec[index] |= (1 << mapping[c])\n",
    "\n",
    "        return sum([i == 7 for i in rec])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        rods = [0] * 10\n",
    "        d = {'R': 1, 'G': 2, 'B': 4}\n",
    "        for i in range(0, len(rings), 2):\n",
    "            rods[int(rings[i + 1])] |= d[rings[i]]\n",
    "        return rods.count(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "\n",
    "\n",
    "        RED, GREEN, BLUE = 0, 1, 2\n",
    "        def c2i(color):\n",
    "\n",
    "            if color == \"R\":\n",
    "                return 0\n",
    "            if color == \"G\":\n",
    "                return 1\n",
    "            else:\n",
    "                return 2\n",
    "\n",
    "        n = len(rings)\n",
    "        res = 0\n",
    "        colors = [set() for _ in range(10)]\n",
    "        for i in range(0, n, 2):\n",
    "            c, idx = rings[i], rings[i + 1]\n",
    "\n",
    "            colors[int(idx)].add(c2i(c))\n",
    "            \n",
    "\n",
    "        for i in range(10):\n",
    "            if len(colors[i]) == 3:\n",
    "                res += 1\n",
    "        \n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, s: str) -> int:\n",
    "        n, ans = len(s), 0\n",
    "        map = [0] * 128\n",
    "        for i in range(0, n, 2):\n",
    "            map[ord(s[i]) - ord('B')] |= 1 << (int(s[i + 1]) - int('0'))\n",
    "        for i in range(10):\n",
    "            tot = 0\n",
    "            for c in ['R', 'G', 'B']:\n",
    "                tot += (map[ord(c) - ord('B')] >> i) & 1\n",
    "            ans += 1 if tot == 3 else 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "\n",
    "        \"\"\"\n",
    "        有 n 個圓環, 每個圓環的顏色有: R, G, B\n",
    "        這 n 個圓環分散在 10 個桿子上面(0~9)\n",
    "\n",
    "        給一個 string: rings\n",
    "        1. 長度是 2n\n",
    "        2. 裡面紀錄的資訊是: <哪個顏色><哪個桿子>\n",
    "        \n",
    "        回傳有幾個桿子上面是有 R, G, B 三種圓環\n",
    "        \"\"\"\n",
    "\n",
    "        \"\"\"\n",
    "        1. 做一個 dict\n",
    "           key = rod_num\n",
    "           value = set(三種顏色), 用 set 不重複\n",
    "        2. 走過一次 rings, 紀錄 key, value\n",
    "        3. 方法:\n",
    "            a. 檢查 dict 每個 key 的 set 長度是否等於 3, 是的話就累積次數\n",
    "            b. 直接做一個 set(\"ABC\"), 把每個 value 都和它比對\n",
    "        \"\"\"\n",
    "        rods = collections.defaultdict(set)\n",
    "\n",
    "        for i in range(0, len(rings), 2):\n",
    "            color   = rings[i]\n",
    "            rod_num = rings[i + 1]\n",
    "            rods[rod_num].add(color)\n",
    "        \n",
    "        result = 0\n",
    "        for i in rods:\n",
    "            if len(rods[i]) == 3:\n",
    "                result += 1\n",
    "        return result "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        r,g,b=[],[],[]\n",
    "        for i in range(len(rings)//2):\n",
    "            if rings[2*i]=='R':\n",
    "                r.append(rings[2*i+1])\n",
    "            elif rings[2*i]=='G':\n",
    "                g.append(rings[2*i+1])\n",
    "            elif rings[2*i]=='B':\n",
    "                b.append(rings[2*i+1])\n",
    "        return len(set(r)&set(g)&set(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        count = 0\n",
    "        for i in range(10):\n",
    "            t1 = False\n",
    "            t2 = False\n",
    "            t3 = False\n",
    "            for j in range(0,len(rings),2):\n",
    "                if str(rings[j + 1]) == str(i) and rings[j] == \"R\":\n",
    "                    t1 = True\n",
    "                if str(rings[j + 1]) == str(i) and rings[j] == \"G\":\n",
    "                    t2 = True\n",
    "                if str(rings[j + 1]) == str(i) and rings[j] == \"B\":\n",
    "                    t3 = True\n",
    "            if t1 and t2 and t3:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        count = 0\n",
    "        for i in range(10):\n",
    "            t1 = False\n",
    "            t2 = False\n",
    "            t3 = False\n",
    "            for j in range(0,len(rings),2):\n",
    "                if rings[j + 1] == str(i) and rings[j] == \"R\":\n",
    "                    t1 = True\n",
    "                if rings[j + 1] == str(i) and rings[j] == \"G\":\n",
    "                    t2 = True\n",
    "                if rings[j + 1] == str(i) and rings[j] == \"B\":\n",
    "                    t3 = True\n",
    "            if t1 and t2 and t3:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        arr = [0] * 10\n",
    "        i = 0\n",
    "        n = len(rings)\n",
    "\n",
    "        while i < n:\n",
    "            color = rings[i]\n",
    "            i += 1\n",
    "\n",
    "            num = ord(rings[i]) & 15\n",
    "            i += 1\n",
    "\n",
    "            match color:\n",
    "                case \"R\":\n",
    "                    arr[num] |= 1\n",
    "                case \"G\":\n",
    "                    arr[num] |= 2\n",
    "                case \"B\":\n",
    "                    arr[num] |= 4\n",
    "        \n",
    "        return arr.count(7)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        counter = [0 for _ in range(10)]\n",
    "        chars = {'R': 0, 'G': 1, 'B': 2}\n",
    "\n",
    "        for i in range(1, len(rings), 2):\n",
    "            counter[int(rings[i])] |= 1 << chars[rings[i - 1]]\n",
    "        \n",
    "        return sum(c == 7 for c in counter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        fin = [[] for i in range(10)]\n",
    "        while len(rings)>0:\n",
    "            fin[int(rings[1])].append(rings[0])\n",
    "            rings=rings[2:]\n",
    "        a=0\n",
    "        for i in fin:\n",
    "            if len(set(i))==3:\n",
    "                a+=1\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        d = {\"R\": 1, \"G\": 2, \"B\": 4}\n",
    "        status = 0\n",
    "\n",
    "        for i in range(0, len(rings), 2):\n",
    "            num = ord(rings[i+1]) & 15\n",
    "            status |= d[rings[i]] << (num * 3)\n",
    "        \n",
    "        ans = 0\n",
    "        mask = 7\n",
    "\n",
    "        for _ in range(10):\n",
    "            ans += status & mask == mask\n",
    "            mask <<= 3\n",
    "        \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        sticks=['000' for i in range(10)]\n",
    "        ans=0\n",
    "        for i in range(0,len(rings),2):\n",
    "            color=rings[i]\n",
    "            number=int(rings[i+1])\n",
    "            if color=='R':\n",
    "                sticks[number]='1'+sticks[number][1:]\n",
    "            elif color=='G':\n",
    "                sticks[number]=sticks[number][0]+'1'+sticks[number][2]\n",
    "            else:\n",
    "                sticks[number]=sticks[number][:2]+'1'\n",
    "        for i in range(10):\n",
    "            if sticks[i]=='111':\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        n = len(rings)/2\n",
    "        rods=[[False,False,False] for _ in range(10)]\n",
    "\n",
    "        for i in range(0,len(rings),2):\n",
    "            color,position=rings[i],int(rings[i+1])\n",
    "            if color == 'R':\n",
    "                rods[position][0] = True\n",
    "            elif color == 'G':\n",
    "                rods[position][1] = True\n",
    "            elif color == 'B':\n",
    "                rods[position][2] = True\n",
    "        count = 0\n",
    "        for r in rods:\n",
    "            if all(r):\n",
    "                count +=1 \n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        ans = 0\n",
    "        if len(rings)<6:\n",
    "            return ans\n",
    "        \n",
    "        temp = [0]*10\n",
    "        #print(temp)\n",
    "        for i in range(0,len(rings)-1,2):\n",
    "            j = int(rings[i+1])\n",
    "            #print(j)\n",
    "            if rings[i] == \"R\":\n",
    "                temp[j] |= 1\n",
    "            if rings[i] == \"G\":\n",
    "                temp[j] |= 2\n",
    "            if rings[i] == \"B\":\n",
    "                temp[j] |= 4\n",
    "        for i in range(10):\n",
    "            if temp[i] == 7:\n",
    "                ans+=1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        dic = {str(i):[] for i in range(10)}#使用[]列表在字典中追加元素\n",
    "        ans = 0\n",
    "        for idx in range(0,len(rings),2):\n",
    "            dic[rings[idx+1]].append(rings[idx])\n",
    "        for key in dic:\n",
    "            if len(set(dic[key])) >= 3:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        mask = [0] * 10\n",
    "        d = {\"R\": 1, \"G\": 2, \"B\": 4}\n",
    "        for i in range(0, len(rings), 2):\n",
    "            c = rings[i]\n",
    "            j = int(rings[i + 1])\n",
    "            mask[j] |= d[c]\n",
    "        return mask.count(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        ad={}\n",
    "        count=0\n",
    "        for i in range(len(rings)):\n",
    "            if i+1<len(rings):\n",
    "                if rings[i+1].isdigit():\n",
    "                    if rings[i+1] not in ad:\n",
    "                        ad[rings[i+1]]=[rings[i]]\n",
    "                    else:\n",
    "                        ad[rings[i+1]].append(rings[i])\n",
    "        for i in ad:\n",
    "            if len(set(ad[i]))==3:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        res = [set() for _ in range(10)]\n",
    "        n = len(rings)//2\n",
    "        for i in range(n):\n",
    "            key = int(rings[2*i+1])\n",
    "            values = rings[2*i]\n",
    "            res[key].add(values)\n",
    "        ans = sum([1 if len(s)==3 else 0 for s in res])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        # 环组个列表，每个环一个列表，存放环上的颜色\n",
    "        r,g,b=[],[],[]\n",
    "        for i in range(0,len(rings),2):\n",
    "            if rings[i]==\"R\":\n",
    "                r.append(rings[i+1])\n",
    "            elif rings[i]==\"G\":\n",
    "                g.append(rings[i+1])\n",
    "            elif rings[i]==\"B\":\n",
    "                b.append(rings[i+1])\n",
    "        print(r)\n",
    "        print(g)\n",
    "        print(b)\n",
    "        res=set(r)&set(g)&set(b)\n",
    "        return len(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        fin = [[] for i in range(10)]\n",
    "        while len(rings)>0:\n",
    "            fin[int(rings[1])].append(rings[0])\n",
    "            rings=rings[2:]\n",
    "        a=0\n",
    "        for i in fin:\n",
    "            if len(set(i))==3:\n",
    "                a+=1\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        count = 0\n",
    "        result = defaultdict(set)\n",
    "        for i in range(0, len(rings), 2):\n",
    "            result[rings[i+1]].add(rings[i])\n",
    "        for _, value in result.items():\n",
    "            if len(value) == 3:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        mp = [0]*10\n",
    "        for x in [rings[2*i:2*i+2] for i in range(len(rings)//2)]:\n",
    "            mp[int(x[1])]|= 4 if x[0]=='B' else 2 if x[0]=='G' else 1\n",
    "        return len([_ for _ in filter(lambda x:x==7, mp)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        count = 0\n",
    "        recode = {key: '' for key in rings[1:][::2]}\n",
    "        for i in range(len(rings) // 2):\n",
    "            recode[rings[2 * i + 1]] += rings[2 * i]\n",
    "        s1 = set('RGB')\n",
    "        for key in recode.keys():\n",
    "            if set(recode[key]) == s1:\n",
    "                count += 1\n",
    "        return count\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        m = defaultdict(set)\n",
    "        for i in range(1,len(rings),2):\n",
    "            m[rings[i]].add(rings[i-1])\n",
    "        ans = 0\n",
    "        for s in m:\n",
    "            if len(m[s]) == 3:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        res = 0\n",
    "        color_dict = defaultdict(set)\n",
    "        for i in range(0, len(rings), 2):\n",
    "            color_dict[rings[i + 1]].add(rings[i])\n",
    "\n",
    "        for d in color_dict.keys():\n",
    "            if len(color_dict[d]) == 3:\n",
    "                res += 1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        import collections\n",
    "\n",
    "        n = len(rings)\n",
    "        i = 0\n",
    "        d = collections.defaultdict(lambda:[0,0,0])\n",
    "        while i < n:\n",
    "            c,p = rings[i],rings[i+1]\n",
    "            if c == 'B':\n",
    "                d[p][2] += 1\n",
    "            elif c == 'G':\n",
    "                d[p][1] += 1\n",
    "            else:\n",
    "                d[p][0] += 1\n",
    "            i += 2\n",
    "        \n",
    "        cnt = 0\n",
    "        for k,v in d.items():\n",
    "            if 0 not in v:\n",
    "                cnt += 1\n",
    "\n",
    "        return cnt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        ring_color_map = {\n",
    "            str(_index_): set() for _index_ in range(10)\n",
    "        }\n",
    "        for ring_index in range(len(rings) // 2):\n",
    "            ring_color_map[rings[2*ring_index+1]].add(rings[2*ring_index])\n",
    "        return sum([len(item) == 3 for key, item in ring_color_map.items()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "        def getColorId(color):\n",
    "            if color == 'R':\n",
    "                return 0\n",
    "            elif color=='G':\n",
    "                return 1\n",
    "            else:\n",
    "                return 2\n",
    "        POLE_NUM,COLOR_NUM = 10,3\n",
    "        state = [[0 for i in range(COLOR_NUM)] for j in range(POLE_NUM)]\n",
    "        n = len(rings)\n",
    "        for i in range(0,n,2):\n",
    "            color = rings[i]\n",
    "            pole_index = ord(rings[i+1])-ord('0')\n",
    "            state[pole_index][getColorId(color)]=1\n",
    "        res = 0\n",
    "        for i in range(POLE_NUM):\n",
    "            flag = True\n",
    "            for j in range(COLOR_NUM):\n",
    "                if state[i][j] == 0:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag:\n",
    "                res +=1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "\n",
    "        ans = 0\n",
    "\n",
    "        d = collections.defaultdict(dict)\n",
    "\n",
    "        for i in range(len(rings)):\n",
    "            if rings[i].isdigit():\n",
    "                d[rings[i]][rings[i-1]] = True\n",
    "        \n",
    "        for v in d.values():\n",
    "            if v.get('R', False) and v.get('G', False) and v.get('B', False):\n",
    "                ans += 1\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPoints(self, rings: str) -> int:\n",
    "\n",
    "        if len(rings) < 6:\n",
    "\n",
    "            return 0\n",
    "\n",
    "        myHash = dict(zip([str(i) for i in range(0,10)],[['R','G','B'] for i in range(0,10)]))\n",
    "\n",
    "        for i in range(0,len(rings),2):\n",
    "\n",
    "            if rings[i + 1] in myHash:\n",
    "\n",
    "                if rings[i] in myHash[rings[i + 1]]:\n",
    "\n",
    "                    ls =  myHash[rings[i + 1]]\n",
    "\n",
    "                    ls = [s for s in ls if s != rings[i]]                    \n",
    "\n",
    "                    myHash[rings[i + 1]] = ls\n",
    "\n",
    "                    if len(ls) == 0:\n",
    "\n",
    "                        myHash.pop(rings[i + 1])\n",
    "\n",
    "        print(myHash)\n",
    "\n",
    "        return 10 - len(myHash)\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
